﻿#include "StdAfx.h"
#include "Regesiter.h"


Regesiter::Regesiter(void)
{
	
}


Regesiter::~Regesiter(void)
{
}



LSTATUS Regesiter::UserInfoRegesiter(
	const std::wstring& serviceProvider, 
	const std::wstring& userName , 
	const std::wstring& userPwd,
	const std::wstring& userreplyPhone,
	const std::wstring& usersmtpAddress
	)
{
	HKEY AddinKey;
	HKEY hKey;bool hKeyGet = false;
	if(RegOpenKeyExW(HKEY_CURRENT_USER,L"Software\\Microsoft\\Office\\Outlook\\Addins\\OutlookAddin.FormAddin",0,KEY_ALL_ACCESS,&AddinKey) != ERROR_SUCCESS) goto err_realse;	
	if(RegOpenKeyExW(AddinKey,serviceProvider.c_str(),0,KEY_ALL_ACCESS,&hKey) != ERROR_SUCCESS) goto err_realse;
	RegCloseKey(AddinKey);
	hKeyGet = true;
	if(RegSetValueExW(hKey,L"USER",0,REG_MULTI_SZ,(BYTE*)userName.c_str(),userName.length()*2+2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(hKey,L"PWD",0,REG_MULTI_SZ,(BYTE*)userPwd.c_str(),userPwd.length()*2+2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(hKey,L"MOBILE",0,REG_MULTI_SZ,(BYTE*)userreplyPhone.c_str(),userreplyPhone.length()*2+2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(hKey,L"MAIL",0,REG_MULTI_SZ,(BYTE*)usersmtpAddress.c_str(),usersmtpAddress.length()*2+2) != ERROR_SUCCESS) goto err_realse;

	RegCloseKey(hKey);
	return ERROR_SUCCESS;

err_realse:
	if(hKeyGet) RegCloseKey(hKey);
	return -1;
}


LSTATUS Regesiter::ServiceInfoRegesiter(
	const std::wstring& serviceProvider,
	const std::wstring& serviceUri,
	const std::wstring& signUpPage,
	const std::wstring& localName,
	const std::wstring& SMS_maxRecipientsPerMessage,
	const std::wstring& SMS_maxMessagesPerSend,
	const std::wstring& SMS_maxSbcsPerMessage,
	const std::wstring& SMS_maxDbcsPerMessage
	)
{
	std::wstring temp = L"Software\\Microsoft\\Office\\Outlook\\Addins\\OutlookAddin.FormAddin\\";
	temp.append(serviceProvider);

	HKEY sKey;
	bool keyGet = false;
	if(RegCreateKeyExW(HKEY_CURRENT_USER,temp.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&sKey,NULL) != ERROR_SUCCESS) goto err_realse;
	keyGet = true;
	if(RegSetValueExW(sKey,L"serviceProvider",0,REG_MULTI_SZ,(BYTE*)serviceProvider.c_str(),(DWORD)serviceProvider.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"serviceUri",0,REG_MULTI_SZ,(BYTE*)serviceUri.c_str(),(DWORD)serviceUri.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"signUpPage",0,REG_MULTI_SZ,(BYTE*)signUpPage.c_str(),(DWORD)signUpPage.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"localName",0,REG_MULTI_SZ,(BYTE*)localName.c_str(),(DWORD)localName.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"SMS_maxRecipientsPerMessage",0,REG_MULTI_SZ,(BYTE*)SMS_maxRecipientsPerMessage.c_str(),(DWORD)SMS_maxRecipientsPerMessage.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"SMS_maxMessagesPerSend",0,REG_MULTI_SZ,(BYTE*)SMS_maxMessagesPerSend.c_str(),(DWORD)SMS_maxMessagesPerSend.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"SMS_maxSbcsPerMessage",0,REG_MULTI_SZ,(BYTE*)SMS_maxSbcsPerMessage.c_str(),(DWORD)SMS_maxSbcsPerMessage.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;
	if(RegSetValueExW(sKey,L"SMS_maxDbcsPerMessage",0,REG_MULTI_SZ,(BYTE*)SMS_maxDbcsPerMessage.c_str(),(DWORD)SMS_maxDbcsPerMessage.length()*2 + 2) != ERROR_SUCCESS) goto err_realse;

	RegCloseKey(sKey);
	return ERROR_SUCCESS;

err_realse:
	if(keyGet) RegCloseKey(sKey);
	DWORD error = ::GetLastError();
	return -1;
}


LSTATUS Regesiter::LoadServiceProvider(std::vector<ServiceProvider*>* list)
{
	//realse previous data
	if(!list) return -1;
	for(std::vector<ServiceProvider*>::iterator i = list->begin();
		i != list->end();
		i++){
		delete (*i);
		(*i) = NULL;
	}
	list->clear();

	//enum regedit

	HKEY AddinKey;
	ServiceProvider* t_provider;
	long result = -1;
	int iter = 0;
	std::wstring temp;
	if(RegOpenKeyExW(HKEY_CURRENT_USER,L"Software\\Microsoft\\Office\\Outlook\\Addins\\OutlookAddin.FormAddin",0,KEY_ALL_ACCESS,&AddinKey) != ERROR_SUCCESS)
		return -1;
	LPWSTR nameBuffer = new wchar_t[256];
	DWORD bufferSize = 256;
	result = RegEnumKeyExW(AddinKey,iter,nameBuffer,&bufferSize,0,NULL,NULL,NULL);
	while(result == ERROR_SUCCESS){
		temp.clear();
		temp.append(nameBuffer);
		t_provider = new ServiceProvider();
		if(LoadSerivceProviderValue(AddinKey,temp,*t_provider) != ERROR_SUCCESS){
			delete t_provider;
			continue;
		}
		list->push_back(t_provider);
		iter++;
		delete[] nameBuffer;
		nameBuffer = new wchar_t[256];
		bufferSize = 256;//pay attention here!!!
		result = RegEnumKeyExW(AddinKey,iter,nameBuffer,&bufferSize,0,NULL,NULL,NULL);
	}
	delete[] nameBuffer;
	return 0;
}




LSTATUS Regesiter::LoadSerivceProviderValue(HKEY AddinKey , const std::wstring& providerName  , ServiceProvider& resultProvider)
{

	HKEY providerKey;
	DWORD dType = REG_MULTI_SZ;
	DWORD bufferSize = 0;
	if(RegOpenKeyExW(AddinKey,providerName.c_str(),0,KEY_ALL_ACCESS,&providerKey) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"serviceProvider",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.ServiceProviderName.clear();
	resultProvider.ServiceProviderName.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"serviceProvider",0,&dType,(LPBYTE)resultProvider.ServiceProviderName.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"serviceUri",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.ServiceUri.clear();
	resultProvider.ServiceUri.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"serviceUri",0,&dType,(LPBYTE)resultProvider.ServiceUri.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"localName",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.LocalName.clear();
	resultProvider.LocalName.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"localName",0,&dType,(LPBYTE)resultProvider.LocalName.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"USER",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.UserName.clear();
	resultProvider.UserName.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"USER",0,&dType,(LPBYTE)resultProvider.UserName.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"PWD",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.UserPwd.clear();
	resultProvider.UserPwd.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"PWD",0,&dType,(LPBYTE)resultProvider.UserPwd.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	//query bufferSize first
	if(RegQueryValueExW(providerKey,L"MOBILE",0,&dType,NULL,&bufferSize) != ERROR_SUCCESS) return -1;
	//then query data
	resultProvider.ReplyPhone.clear();
	resultProvider.ReplyPhone.resize(bufferSize);
	if(RegQueryValueExW(providerKey,L"MOBILE",0,&dType,(LPBYTE)resultProvider.ReplyPhone.data(),&bufferSize) != ERROR_SUCCESS) return -1;

	return ERROR_SUCCESS;
}